<?
require_once('functions.php');

function GetUsers()
{
    $con = GetConnection();
    $db = mysql_select_db("n02447784_db",$con);
    $sql = "SELECT * from Users";
    return $results = mysql_query($sql,$con);
    
}
function BlankUser()
{
    return array('id'=>null,'Key_Words_id'=>null, 'created_at'=>null,'updated_at'=>null,'first_name'=>null,'last_name'=>null,'phone_number'=> null);
}
function GetUser($id)
{
    $con = GetConnection();
    $db = mysql_select_db("n02447784_db",$con);
    $sql = "SELECT * FROM Users WHERE id=$id";
    return $results = mysql_query($sql, $con);
}
function DeleteUser($id)
{
    $con = GetConnection();
    $db = mysql_select_db("n02447784_db",$con);
    $sql = mysql_query("DELETE FROM Users WHERE id=$id");
    return $results = mysql_query($sql, $con);
}
function SaveUser(&$row)
{
    $con = GetConnection();
	$db = mysql_select_db("n02447784_db",$con);
    if(empty($row['id']))
    {
        $sql =  "INSERT INTO Users(Key_Words_id, first_name,last_name,phone_number) 
               VALUES ('$row[Key_Words_id]', '$row[first_name]', '$row[last_name]', '$row[phone_number]' ) ";
        }else{
        $sql =  "UPDATE Users 
               SET Key_Words_id = '$row[Key_Words_id]', updated_at = NOW(), first_name = '$row[first_name]', last_name = '$row[last_name]', phone_number = '$row[phone_number]' 
               WHERE id=$row[id] ";
    }
    	$return = mysql_query($sql,$con);
				
				mysql_close($con);
				return $return;
}
function ValidateUser($row)
{
    $errors = array();
	if(empty($row['Key_Words_id'])) $errors['Key_Words_id'] = 'Position is required';
    if(empty($row['first_name'])) $errors['first_name'] = 'First Name is required';
    if(empty($row['last_name'])) $errors['last_name'] = 'Last Name is required';
	if(empty($row['phone_number'])) $errors['phone_number'] = 'Phone Number is required';   
    return count($errors) == 0 ? true : $errors;
}
?>